#!/bin/sh
#
# Setup script for volatile file system.
#
# The script mounts a tmpfs file system on a specified directory and
# recreates some directories inside. Symlinks to "volatile" directories
# must be created in the real fs. Calling "volatile.sh setup" will do
# the trick. Volatile fs resides on RAM and therefore its content will
# be erased at each shutdown.
#
# The script must be placed in /etc/init.d and symlinks created
# in /etc/rc*.d.
# If the directories to be put on volatile file systems reside on a separate
# fs other than the root fs, the script must be invoked after the fs
# is mounted. On Debian GNU/Linux Lenny a good place could be
# /etc/init.d/rcS.d/S04volatile.sh, just after /dev fs has been populated
# by udev.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License
# http://www.gnu.org/licenses/gpl.txt
# This program is distributed WITHOUT ANY WARRANTY.
#
# $Id: volatile.sh 8 2009-08-16 20:36:50Z pietro.pizzo@gmail.com $
#############################################################################

# Directory to be use as volatile fs mount point.
# If the directory does not exist, it will be created.
volatile_dir=/var/volatile

# Directories to put on volatile fs.
# Format: "dir1,mode1,owner1 dir2,mode2,owner2 ... dirn,moden,ownern"
# (space separated list of comma separated directory names, modes and owners)
dirs="/tmp,1777,root:root /var/tmp,1777,root:root /var/log,755,root:root /var/log/fsck,755,root:root /var/log/apt,755,root:root /var/log/gdm,755,root:root /var/log/samba,750,root:adm /var/log/exim4,2750,Debian-exim:adm /var/log/apache2,750,root:adm /var/lock,755,root:root /var/run,755,root:root"

# You may not want to modify anything beyond this point
#######################################################

prog_name=`basename $0`

start()
{
    echo "Setting up volatile file system."

    # Check and create volatile mount point
    if [ -e ${volatile_dir} -a ! -d ${volatile_dir} ]; then
        echo "${prog_name}: Error: \"${volatile_dir}\" exists and it's not a directory. Exiting." >&2
        exit 1
    fi

    if [ ! -e ${volatile_dir} ]; then
        mkdir ${volatile_dir}
    fi

    # Mount tmpfs
    mount |grep -q ${volatile_dir}
    if [ "$?" -ne "0" ]; then
        mount -t tmpfs -o defaults,noatime tmpfs ${volatile_dir}
        if [ $? -ne "0" ]; then
            echo "${prog_name}: Error: tmpfs mount failed. Exiting." >&2
            exit 2;
        fi
    fi

    # Create nested dirs
    for i in ${dirs}; do
        dir=`echo $i |cut -d"," -f1`
        mode=`echo $i |cut -d"," -f2`
        owner=`echo $i |cut -d"," -f3`

        mkdir -p -m ${mode} ${volatile_dir}${dir}
        chown ${owner} ${volatile_dir}${dir}
    done
}

stop()
{
    # We're not going to do anything actually...
    # The volatile fs should be mounted as long as possible.
    echo "Stopping volatile file system"
}

# The setup should be called only once to make the necessary symlinks
setup()
{
    start

    # Move dirs
    for i in ${dirs}; do
        dir=`echo $i |cut -d"," -f1`

        # Move directory and replace if with symlink
        if [ ! -L ${dir} ]; then
            cd ${volatile_dir}${dir}/..
            rmdir `basename ${dir}`
            mv ${dir} .
            ln -s ${volatile_dir}${dir} ${dir}
        fi
    done
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        start
        stop
    ;;
    setup)
        setup
    ;;
    *)
        echo "Usage: ${prog_name} {start|stop|setup}" >&2
        exit 10
    ;;
esac

